﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MultithreadFileCopy;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using System.Threading;
using System.Windows.Threading;

namespace DemoProject
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<MultithreadFile> Files;

        public MainWindow()
        {
            InitializeComponent();

            Files = new ObservableCollection<MultithreadFile>();
            _listView.ItemsSource = Files;
            INIEvents();
        }

        private void INIEvents()
        {
            _btn_getSource.Click += BrowseFile;
            _btn_getDestination.Click += BrowseFolder;
            _btn_addFile.Click += AddNewFile;
            _btn_Start.Click += StartProcess;
        }

        private void StartProcess(object sender, RoutedEventArgs e)
        {
            try
            {
                MultithreadCopySystem system = new MultithreadCopySystem();
                foreach (var v in Files)
                    system.Add(v);

                system.OnError += HandleErrors;
                system.OnProgressReport += HandleProgress;
                system.OnProcessCompleted += HandleCompleted;
                system.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void HandleCompleted(object sender, List<MultithreadFile> e)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                Files.Clear();
                MessageBox.Show(string.Format("Error: {0}\nSkip :{1}\nSucess:{2}", 
                    e.Where(z=>z.Status == FileStatus.Error).Count(),
                    e.Where(z=>z.Status == FileStatus.Skiped).Count(),
                    e.Where(z=>z.Status == FileStatus.Sucess).Count()), "Completed!");
            });
        }
        private void HandleProgress(object sender, ProgressReportArgs e)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                _Prog_Geral.Maximum = e.TotalBytes;
                _Prog_Geral.Value = e.CompletedBytes;

                _Prog_File.Maximum = e.FileTotalBytes;
                _Prog_File.Value = e.FileCompletedBytes;

                _lbl_ProgressFile.Content = string.Format("{0}%", e.FileProgress);
                _lbl_ProgressGeral.Content = string.Format("{0}%", e.OverallProgress);
            });
        }
        private void HandleErrors(object sender, MultithreadFileCopyErrorArgs e)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                MessageBox.Show(e.Description, e.Error);
            });
        }

        private void AddNewFile(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_txtBox_Destination.Text) || string.IsNullOrEmpty(_txtBox_source.Text))
                return;

            try
            {
                MultithreadFile file = new MultithreadFile(_txtBox_source.Text, _txtBox_Destination.Text);
                if (!Files.Contains(file))
                    Files.Add(file);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void BrowseFolder(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_txtBox_source.Text))
                return;

            SaveFileDialog diag = new SaveFileDialog();
            diag.FileName = _txtBox_source.Text;

            if (diag.ShowDialog().Value)
                _txtBox_Destination.Text = diag.FileName;
        }
        private void BrowseFile(object sender, RoutedEventArgs e)
        {
            OpenFileDialog diag = new OpenFileDialog();
            diag.Filter = "AllFiles|*.*";

            if (diag.ShowDialog().Value)
            {
                _txtBox_source.Text = diag.FileName;
            }
        }
    }
}
